<?php namespace App\Controllers;
/**
 * @author Andy Brookes 
 * @author Andy Brookes <andybrookestar  at gmail dot   com>
 */
use CodeIgniter\Controller;
use \App\Models\BlogModel;
use \App\Andy\Utility;
use \App\Andy\ImgPreCheck;
use \App\Andy\BlogManager;



class Blog  extends BaseController
{
		
     protected $blogId;
     protected $myDate;
     protected $myTime;
     protected $blogImages="blogImages";

				 public function __construct() 
				    {
						parent::__construct();
						$this->myTime = parent::getTime();
						$this->myDate= date("d/m/Y",$this->myTime); 	
					
			       }

                   public function altMethod()
                   {
					$BlogHandle = new BlogManager(model(BlogModel::class));
					$result= $BlogHandle->list();
					 }
                   

					public function doCount()
					{
					$handle = new BlogModel();	
					$mycount= $handle->count();
					}		 

					public function doPaginate()
					{
					
					 $handle = new BlogModel();	
					 $data = [
						 'title'=>'paginate',
						'blogs' => $handle->paginate(5),
						'pager' => $handle->pager,
						'date'=>$this->myDate
					];
					echo view('listBlogsPaginate',$data);
					}		

		
					public function showArticle($theSlug)
					
					{
					 $handle = new BlogModel();
					$result= $handle->getArticle($theSlug);
					$data =[
					'title'=>$theSlug,
					'blog'=>$result,
					'date'=>$this->myDate
					];
					echo view('blogArticle',$data);
					}
					
		
					public function listBlogs()
					
					
					{
					
					$handle = new BlogModel();
					$result= $handle->getAll();
					$data =[
					'title'=>'List of Blogs ',
					'blogs'=>$result,
					'date'=>$this->myDate
					];
					echo view('listBlogs',$data);
									
					}
		
		         	public function blogForm()
							{
							// displays form to submit newblog
							$data =[
							'title'=>'new blog',
							'info'=>'&nbsp;',
							'date'=>$this->myDate
							];
							echo view('form4',$data);
					}
		
		          public function newBlogArticle()
	    {
	         	//accepts post data from form4 and processes form submission of data for new blog 
				
				$title = $this->request->getVar('title'); 
			   	$date=  $this->request->getVar('date');
			    $article = $this->request->getVar('article');
		   	    
		   	    //now to remove ALL bad chars, can always do an edit 
		   	    $utilityHandle= new Utility();
		   	     $cleanTitle=$utilityHandle->badCharsRemoveAll($title);
		   	     $cleanDate= $utilityHandle->badCharsRemoveAll($date);
		   	     $cleanArticle1 = $utilityHandle->removeScript($article);
		   	     $cleanArticle2= $utilityHandle->convertQuotes($cleanArticle1);
		   	     $cleanArticle = $utilityHandle->newRegex($cleanArticle2);
		   	     $file = $this->request->getFile('userfile');
		   		$name=   $file->getClientName();
		        $tempfile = $file->getTempName();
		    	$slug= url_title($cleanTitle);
	           //nowsome pre checks 
			  $handle = new ImgPreCheck();
			  //ImgPreCheck is one of my classes
			   
	          	  $logic = $handle->allReadyExists($this->blogImages,$name);
				  if($logic == "true")
				 
				 {
					$data =[
					'title'=>'andything',
					'info'=>'image with that name already exists',
					'date'=>$this->myDate
					];
					echo view('form4',$data);
					exit();
				 }
				 
	
				$logic2 = $handle->extCheck($name);
					
				if($logic2 == 0)
				
				{
				
					$data =[
					'title'=>'andything',
					'info'=>'that is not an image file',
					'date'=>$this->myDate
					];							
						
					echo view('form4',$data);
					 exit();
				
				}
	
					 $size= $handle->getFileSize($tempfile);
				
				if($size > 250000)
				// here i limit upload size of image to 250kb because if your not careful the gigs soon mount up and affects web page load time !
				{
				$data =[
					'title'=>'andything',
					'info'=>'thats too large an image file',
					'date'=>$this->myDate
					];
					echo view('form4',$data);
				    exit();			
				}
	
	   
	  try{
	  
	// $file->move('blogImages',$name);
	$file->move(FCPATH.'blogImages'  );
	 $handle = new BlogModel();
      $blogLogic=	$handle->insertBlog($cleanTitle,$cleanArticle,$name,$slug,$cleanDate);
        }
	 
	catch ( \Exception  $e)
			 {
			
			 $data = [
						'title'  => 'info',
					  
				   'info'=>"somwething went wrong with new blog.". $e->getMessage() ,
				   //take out  $e->getMessage()  when going to production
				   'date'=>$this->myDate
					 ];
                 echo view('info', $data);
				die();
					 
			 
		   }
	 
	 
	 
	 if ($blogLogic == true)
	 	 {
	   $data =[
		'title'=>'blog submission',
		'info'=>'looks like new blog was created, to do something else go to /admin while your logged in  ',
		'date'=>$date
		];
				
  	        
		echo view('info',$data);
		
	 exit();
	
	}
	   
	   
	   
	   
	    }//end new blog 
	    
	    public function delBlogForm()
	      {
	     
	   
	     
	     
	      helper('form');
	     helper('html');
	    
	    $handle = New BlogModel();
	    $result= $handle->getAll();
	    
	    
	    
	    $data =[
		'title'=>'andything',
		'blogList'=>$result,
		'info'=> '',
		'date'=>$this->myDate
		
		
		];
				
  	        
		echo view('removeBlogForm',$data);
		
	    
	    //end of newblog below
	    
	    }
	    
	    public function delBlog()
	    
	    {
			
	 
			
	  helper('uri');
		helper('html');
		helper('form');
	helper('security');  
	    $this->blogId  = $this->request->getVar('BlogId'); 
	    $handle= new BlogModel();
	    $result=  $handle->getOneToDel($this->blogId);
	    
	     if($result==null)
	   {
		 $handle= new BlogModel(); 
		$result= $handle->getAll();  
		  $data = [
                'title'  => 'del ',
                'blogList'=>$result,
                'info'=>'no such item  ',
                'date'=>$this->myDate
		];
		 echo view('removeBlogForm',$data);
       	 exit();  
		   
	   }
	     
	    $image=  $result['image'];
	    //first get rid of image 
	  
	  try
	  {
	  //  chmod(ROOTPATH.'public/blogImages/'.$image,0777);
	      unlink(ROOTPATH.'public/blogImages/'.$image);
	    //now need to remove entry from databse
	  $logic=  $handle->deleteOne($this->blogId);
	  
	  }
	  
	  catch ( \Exception  $e)
			 {
			 $data = [
						'title'  => 'info',
					  
				   'info'=>'somwething went wrong with deleting blog'.$e->getMessage(),
				   'date'=>$date
					 ];
				echo view('info', $data);
				 die();
			 //above try catch works 
		   }
	  
	  
	  
	  
	   if ($logic == True)
	   {
		  $data =[
		'title'=>'del blog',
		'info'=>'lookes like blog was deleted , to do morev while logged in go to /admin',
		'date'=>$this->myDate
		
		];
				
  	       
		echo view('info',$data);
		
		   
	 } 	   
	  
	    }
	    
	    
	    public function editBlogForm()
	      {
			//will try utility class to strip bad chars   
			//here another approach is to first convert all quotes to html entitiy 
			//then in a second round remove bad chars
	    $handle = new BlogModel();
		$result= $handle->getAll();
		$data =[
		'title'=>'andything',
		'blogs'=>$result,
		'date'=>$this->myDate
		];
				
  	       
		echo view('editBlogForm',$data);
		
	    
	    	    
	    }
	    
	    
	    
	    
	    public function editBlog($slug)
	    
	    {
	   
	    
	    
	    $handle= new BlogModel();
	    $result= $handle->getArticle($slug);
	    
	   
	   
	   $data =[
		'title'=>'editing blog ',
		'blog'=>$result,
		'date'=>$this->myDate
		];
				
  	       
		echo view('doEditBlog',$data);
		
	   
	   
	    }
	    
	    
	    
	    public function processEditBlog()
	    
	    {
			
			
			
	  
	    
	     $title = $this->request->getVar('title'); 
	   $article = $this->request->getVar('article');
	   $blogId= $this->request->getVar('Id');
	    $slug= url_title($title);
	   
	   $utilityHandle= new Utility();
	   
	   $cleanTitle=$utilityHandle->badCharsRemoveAll($title);
	   
	  $cleanArticle1 = $utilityHandle->removeScript($article);
	$cleanArticle2= $utilityHandle->convertQuotes($cleanArticle1);
	$cleanArticle3 = $utilityHandle->newRegex($cleanArticle2);
	  
	   
	   
	   
	   
	   try
	   {
	   $handle= new BlogModel();
	   
	$result=   $handle->amendBlog($blogId,$cleanTitle,$cleanArticle3,$slug);
   }
   
   catch ( \Exception  $e)
			 {
			 $data = [
						'title'  => 'info',
					  
				   'info'=>'somwething went wrong with editing blog ',
				   'date'=>$this->myDate
					 ];

				
				echo view('info', $data);
				
				
				  
		   
			 die();
			
		   }
   
   
   
   
	   
	     if ($result == 1) 
	     {
	     
	     
	     $data =[
		'title'=>'blog result',
		'info'=>'lookes like edit of blog was successful, to something else while logged in go to /admin ',
		'date'=>$this->myDate
		];
				
  	       
		echo view('info',$data);
		
	     
	     
	     }
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    }
	    
	    
	    
	   
	    
	    
	    
	    

}
